﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;
using Oplaco.Core.DAL;
using Oplaco.Core.Configuration;
using Oplaco.Core.BusinessLogicLayer;
using System.Collections;
using System.Data;

namespace Oplaco.Web.DTO
{
    public class CachedCategoryCollection : ICollection<CachedCategory>
    {

        #region Members
        private bool isRO;
        private List<CachedCategory> innerCollection;
        #endregion

        #region Property
        public int Count
        {
            get { if (innerCollection == null) return -1;
                else return innerCollection.Count; }
        }

        public bool IsReadOnly
        {
            get { return isRO; }
        }
        #endregion

        #region Construct
        // Adds an index to the collection.
        public CachedCategory this[int index]
        {
            get { return (CachedCategory)innerCollection[index]; }
            set { innerCollection[index] = value; }
        }

        public CachedCategoryCollection()
        {
            innerCollection = new List<CachedCategory>();
        }

        

        

        #endregion
                
        #region Private
        

        
        #endregion

        #region Public
        public void Add(CachedCategory item)
        {
            if (!Contains(item))
            {
                innerCollection.Add(item);
            }
            else
            {
                Oplaco.Exceptions.AppService.ProcessAndNotifyDataAccessLayerException(new Exception("Item is already in the collection" + item.Id.ToString()));
            }
        }

        public void Insert(CachedCategory item, int index) {
            if (!Contains(item))
            {
                innerCollection.Insert(index, item);
            }
            else {
                Oplaco.Exceptions.AppService.ProcessAndNotifyDataAccessLayerException(new Exception("Item is already in the collection" + item.Id.ToString()));
            }
        }

        public void Clear()
        {
            innerCollection.Clear();
        }

        

        public bool Contains(CachedCategory item)
        {
            if (innerCollection == null || item==null) return false;
            CachedCategory found = (from c in innerCollection where c.Equals(item) select c).SingleOrDefault<CachedCategory>();
            return (found == null) ? false : true;
        }

        public void CopyTo(CachedCategory[] array, int arrayIndex)
        {
            innerCollection = (from item in array select item).ToList<CachedCategory>();
        }


        public bool Remove(CachedCategory item)
        {
            CachedCategory found = (from c in innerCollection where c.Equals(item) select c).Single<CachedCategory>();
            if (found == null) return false;
            innerCollection.Remove(found);
            return true;
        }

        public IEnumerator<CachedCategory> GetEnumerator()
        {
            return new CategoryCacheEnumerator(this);
        }

        #endregion

        IEnumerator IEnumerable.GetEnumerator()
        {
            return new CategoryCacheEnumerator(this);
        }


        #region Inner class
        /// <summary>
        /// Defines the enumerator for the CategoryCache collection.
        /// (Some prefer this class outside in the collection class.)
        /// </summary>
        public class CategoryCacheEnumerator : IEnumerator<CachedCategory>
        {
            private CachedCategoryCollection _collection;
            private int curIndex;
            private CachedCategory curItem;

            /// <summary>
            /// 
            /// </summary>
            /// <param name="collection"></param>
            public CategoryCacheEnumerator(CachedCategoryCollection collection)
            {
                _collection = collection;
                curIndex = -1;
                curItem = default(CachedCategory);

            }

            /// <summary>
            /// 
            /// </summary>
            /// <returns></returns>
            public bool MoveNext()
            {
                //Avoids going beyond the end of the collection.
                if (++curIndex >= _collection.Count)
                {
                    return false;
                }
                else
                {
                    // Set current box to next item in collection.
                    curItem = _collection[curIndex];
                }
                return true;
            }

            public void Reset() { curIndex = -1; }

            void IDisposable.Dispose() { }

            public CachedCategory Current
            {
                get { return curItem; }
            }


            object IEnumerator.Current
            {
                get { return Current; }
            }

        }
        #endregion
    }
}
